﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TenderSampleApp.Models.Profile;
using System.Web.Security;
using System.Web.Profile;

namespace TenderSampleApp.Code
{
    public class ProfileMethod
    {
        #region UserProfile Methods
        public bool IsEqualsJuridicalUserProfile(JuridicalUserProfileModel first, JuridicalUserProfileModel second)
        {
            if (second == null)
                return false;

            return (first.Apartment == second.Apartment &&
               first.Build == second.Build &&
               first.City == second.City &&
               first.District == second.District &&
               first.Name == second.Name &&
               first.Index == second.Index &&
               first.Phone == second.Phone &&
               first.Region == second.Region &&
               first.Street == second.Street &&
               first.Code == second.Code);
        }

        public bool IsEqualsPhysicalUserProfile(PhysicalUserProfileModel first, PhysicalUserProfileModel second)
        {
            if (second == null)
                return false;
            return (first.Apartment == second.Apartment &&
                first.BirthdayYear == second.BirthdayYear &&
                first.Build == second.Build &&
                first.CertificateIssueBy == second.CertificateIssueBy &&
                first.CertificateSeries == second.CertificateSeries &&
                first.City == second.City &&
                first.DateOfIssueСertificate == second.DateOfIssueСertificate &&
                first.District == second.District &&
                first.FirstName == second.FirstName &&
                first.Index == second.Index &&
                first.Lastname == second.Lastname &&
                first.NumberCertificate == second.NumberCertificate &&
                first.Phone == second.Phone &&
                first.Region == second.Region &&
                first.Street == second.Street &&
                first.Surname == second.Surname
         );
        }

        public void FillPhysicalUserProfileMethod(PhysicalUserProfileModel userprofile)
        {
            MembershipUser user = Membership.GetUser();
            ProfileBase profile = ProfileBase.Create(user.UserName, user.IsApproved);

            profile.SetPropertyValue("Lastname", userprofile.Lastname);
            profile.SetPropertyValue("FirstName", userprofile.FirstName);
            profile.SetPropertyValue("Surname", userprofile.Surname);
            profile.SetPropertyValue("Phone", userprofile.Phone);
            profile.SetPropertyValue("Index", userprofile.Index);
            profile.SetPropertyValue("Region", userprofile.Region);
            profile.SetPropertyValue("District", userprofile.District);
            profile.SetPropertyValue("City", userprofile.City);
            profile.SetPropertyValue("Street", userprofile.Street);
            profile.SetPropertyValue("Build", userprofile.Build);
            profile.SetPropertyValue("Apartment", userprofile.Apartment);
            profile.SetPropertyValue("BirthdayYear", userprofile.BirthdayYear);
            profile.SetPropertyValue("CertificateSeries", userprofile.CertificateSeries);
            profile.SetPropertyValue("NumberСertificate", userprofile.NumberCertificate);
            profile.SetPropertyValue("CertificateIssueBy", userprofile.CertificateIssueBy);
            profile.SetPropertyValue("DateOfIssueСertificate", userprofile.DateOfIssueСertificate);
            profile.Save();
        }

        public void FillJuridicalUserProfileMethod(JuridicalUserProfileModel userprofile)
        {
            MembershipUser user = Membership.GetUser();
            ProfileBase profile = ProfileBase.Create(user.UserName, user.IsApproved);

            profile.SetPropertyValue("Name", userprofile.Name);
            profile.SetPropertyValue("Phone", userprofile.Phone);
            profile.SetPropertyValue("Index", userprofile.Index);
            profile.SetPropertyValue("Region", userprofile.Region);
            profile.SetPropertyValue("District", userprofile.District);
            profile.SetPropertyValue("City", userprofile.City);
            profile.SetPropertyValue("Street", userprofile.Street);
            profile.SetPropertyValue("Build", userprofile.Build);
            profile.SetPropertyValue("Apartment", userprofile.Apartment);
            profile.SetPropertyValue("Code", userprofile.Code);

            profile.Save();
        }

        public PhysicalUserProfileModel GetPhysicalUserProfileMethod()
        {
            MembershipUser user = Membership.GetUser();
            ProfileBase profile = ProfileBase.Create(user.UserName, user.IsApproved);
            PhysicalUserProfileModel userprofile = new PhysicalUserProfileModel()
            {
                Lastname = profile.GetPropertyValue("Lastname").ToString(),
                FirstName = profile.GetPropertyValue("FirstName").ToString(),
                Surname = profile.GetPropertyValue("Surname").ToString(),
                Phone = profile.GetPropertyValue("Phone").ToString(),
                Index = profile.GetPropertyValue("Index").ToString(),
                Region = profile.GetPropertyValue("Region").ToString(),
                District = profile.GetPropertyValue("District").ToString(),
                City = profile.GetPropertyValue("City").ToString(),
                Street = profile.GetPropertyValue("Street").ToString(),
                Build = profile.GetPropertyValue("Build").ToString(),
                Apartment = profile.GetPropertyValue("Apartment") as int?,
                BirthdayYear = Convert.ToInt32(profile.GetPropertyValue("BirthdayYear")),
                CertificateSeries = profile.GetPropertyValue("CertificateSeries").ToString(),
                NumberCertificate = profile.GetPropertyValue("NumberСertificate").ToString(),
                CertificateIssueBy = profile.GetPropertyValue("CertificateIssueBy").ToString(),
                DateOfIssueСertificate = Convert.ToDateTime(profile.GetPropertyValue("DateOfIssueСertificate"))
            };
            return userprofile;
        }

        public JuridicalUserProfileModel GetJuridicalUserProfileMethod()
        {
            MembershipUser user = Membership.GetUser();
            ProfileBase profile = ProfileBase.Create(user.UserName, user.IsApproved);
            JuridicalUserProfileModel userprofile = new JuridicalUserProfileModel()
            {
                Name = profile.GetPropertyValue("Name").ToString(),
                Code = profile.GetPropertyValue("Code").ToString(),
                Phone = profile.GetPropertyValue("Phone").ToString(),
                Index = profile.GetPropertyValue("Index").ToString(),
                Region = profile.GetPropertyValue("Region").ToString(),
                District = profile.GetPropertyValue("District").ToString(),
                City = profile.GetPropertyValue("City").ToString(),
                Street = profile.GetPropertyValue("Street").ToString(),
                Build = profile.GetPropertyValue("Build").ToString(),
                Apartment = profile.GetPropertyValue("Apartment") as int?,
            };
            return userprofile;
        }
        #endregion

    }
}